//////////////////////////////////////////////////////////////////
//
//
//		AX, ringbuffer wrap
//
//		
//		
//
//
//
#include "RBuffer.h"
#include "ringbuffer.h"
#include "ScopedCritics.h"

#include <assert.h>

#define RBF ((RingBuffer*)m_pRingBuffer)
typedef cmm_base::scoped_ptr<cmm_base::CriticalSection>  AutoCritics;
RBuffer::RBuffer(int nBufferSize )
{
	m_pRingBuffer = 0;
 	if(((nBufferSize -1) & nBufferSize ) != 0)
		throw -1;

	m_pRingBuffer = new RingBuffer;
	char* pDataPtr    = new char [nBufferSize ];

	RingBuffer_Init(RBF,nBufferSize,pDataPtr);
}

RBuffer::~RBuffer()
{
	if(m_pRingBuffer)
	{
		if(RBF->buffer)
			delete RBF->buffer;
		delete m_pRingBuffer;
	}
 
	m_pRingBuffer = 0;
 
}


//return read size
int RBuffer::Read(char* pBuf,int nSize)
{
	assert(RBF);
	AutoCritics al(&m_cs);
	return RingBuffer_Read(RBF,pBuf,nSize);
}

//return write size
int RBuffer::Write(const char* pData,int nSize)
{
	assert(RBF);
	AutoCritics al(&m_cs);
	return RingBuffer_Write(RBF,(void*)pData,nSize);
}

//data len in ring buffer
int RBuffer::GetAvailableReadSize()
{
	return RingBuffer_GetReadAvailable(RBF);
}

//writeable size in ringbuffer,just remain size
int RBuffer::GetAvailableWriteSize()
{
	return RingBuffer_GetWriteAvailable(RBF);
}

//ring buffer total size
int RBuffer::GetBufferSize()const
{
	return RBF->bufferSize;
}
void RBuffer::Reset()
{
	assert(RBF);
	AutoCritics al(&m_cs);
	RingBuffer_Flush(RBF);
}
//
//private:
//	void*         m_pRingBuffer;
//	char*         m_pDataPtr;

 
